const { participateIn } = require("../../../data/activity/activityForIndex");
const { formatTime } = require('../../../utils/util.js')
const app = getApp()
// pages/elderly/activityDetail/activityDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAuthor: null,
    userid:null,
    activity:undefined,
    isParticipate: null,
    allPart:null,
    nowPartId:null,
    nowParterId: null,

    show:false,  //弹出框
    phone:null,

    showP:false,
    radio:null,
    accuseForm:{
      type:null,
      memo:'',
      articleid:null,
      fromsource:2
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //根据点击的活动id，从缓存中读取活动
    var v = 'accuseForm.articleid'
    var activityId = options.activityId
    this.setData({
      userid:app.globalData.userInfo.userId,
      [v]: activityId
    })
    console.log("*",options.activityId)
    // var activity = this.findActivityById(activityId)
    this.findActivityById(activityId)
    this.getActivityPart(activityId)
    this.getAllPart(activityId)
    
    console.log("发起人",this.data.isAuthor)
    console.log("参与",this.data.isParticipate)
    console.log("参与ID ",this.data.nowPartId)
  },
  getAllPart(activityid){
    app.sendGetRequest('/heart/myAllPart/'+ activityid, (res)=>{
      this.setData({
        allPart: res.rows
      })
    },(res)=>{
      console.log(res)
    })
  },
  findActivityById:function(activityId){
    var that = this;
    app.sendGetRequest('/heart/activity/'+activityId, (res) => {
      that.setData({
        activity:res
      })
      console.log("userid",that.data.userid)
      if( (that.data.userid!=null) && (that.data.userid == res.initiatorid) ){
        that.setData({
          isAuthor: true
        })
      } else {
        that.setData({
          isAuthor: false
        })
      }
    })
  },
  getActivityPart:function(activityid){
    var that = this
    app.sendGetRequest('/heart/activity/part/'+activityid, (res) => {
      console.log("part", res)
      if(res.participatorid==null){
        that.setData({
          isParticipate: false,
        })
      }else{
        console.log("res.participatorid",res.participatorid)
        console.log("that.data.userid",that.data.userid)
        that.setData({
          nowPartId: res.id,
          nowParterId: res.participatorid
        })
        console.log("res.id ",res.id)
        console.log("res.participatorid ",res.participatorid)
        if(res.participatorid==that.data.userid){
          that.setData({
            isParticipate: true,
          })
          console.log("+=================")
          console.log(that.data.isParticipate)
        }
      }
    })
  },
  canyu(){
    this.setData({
      show: !this.data.show
    })
  },
  doCanyu(){
    let temp = 'activity.statusflag'
    let data = {
      activityid: this.data.activity.activityid,
      intime: formatTime(new Date()),
      phone: this.data.phone
    }
    console.log("data",data)
    app.sendPostRequest('/heart/activity/participate', data, (res) => {
      this.setData({
        show: false
      })
      if(res.code==200){
        wx.showToast({
          title: '参与成功',
        })
      }else if(res.code==300){
        wx.showToast({
          title: '有人参与',
          icon:'error'
        })
      }
    }, (err) => {
      console.log(err)
    })
    if(this.data.activity.statusflag == 0 && !this.data.isParticipate){
      this.setData({
        isParticipate:true,
        [temp]:1
      })
    }
    console.log(this.data.activity)
  },
  phoneChange(e){
    var cur = e.detail
    this.setData({
      phone: cur
    })
  },
  submit(){
    if(this.data.phone==null){
      wx.showToast({
        title: '手机号错误',
        icon:'error'
      })
      return;
    }
    this.doCanyu()
  },
  deletePart(e){
    console.log(e.currentTarget)
    var v = '拒绝'+ e.currentTarget.dataset.partname + '参加'
    wx.showActionSheet({
      itemList: [v],
      success: function(res) {
          // console.log(res.tapIndex)
          wx.showModal({
            title: '提示',
            content:"拒绝之后该用户12小时不能参与",
            success:function(){
              console.log(e.currentTarget.dataset.partid)
              console.log("that.data.nowParterId",that.data.nowParterId)

              let data = {
                id: e.currentTarget.dataset.partid,
                outtime: formatTime(new Date()),
                id: that.data.nowPartId,
                statusflag: 3,
                participatorid: that.data.nowParterId
              }
              app.sendPostRequest('/heart/remotePart', data, (res)=>{
                if(res==1){
                  wx.showToast({
                    title: '拒绝完成',
                  })
                }else {
                  wx.showToast({
                    title: '已经拒绝',
                    icon: 'error'
                  })
                }
              },(err)=>{
                console.log(err)
              })
            },
            confirmText:'拒绝参与',
            cancelText:"我再想想",
            confirmColor: "#f20c00"
          })
      },
      fail: function(res) {
          console.log(res.errMsg)
      }
    })
  },
  finish(){
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定该活动已完成吗？',
      success: function(res) {
        var HeartActivityDetail = {
          statusflag: 2,
          endtime: formatTime(new Date()),
          activityid: that.data.activity.activityid
        }
        app.sendPostRequest("/heart/activity/finish", HeartActivityDetail, (res)=>{
          console.log("更新活动",res)
          if(res == 1){
            if(that.data.nowPartId!=null){
              let HeartActivityPart = {
                id: that.data.nowPartId,
                statusflag: 2,
                outtime:formatTime(new Date()),
                activityid: that.data.activity.activityid,
                participatorid: that.data.nowParterId
              }
              app.sendPostRequest("/heart/activity/updataNowPart", HeartActivityPart, (res)=>{
                console.log("更新参与者",res)
                if(res == 1){
                  wx.showToast({
                    title: '活动完成成功',
                    duration: 1500,
                    success:function () {
                      setTimeout(function(){
                        var pages = getCurrentPages(); //当前页面
                        var beforePage = pages[pages.length - 2]; //前一页
                        beforePage.onLoad(); // 执行前一个页面的onLoad方法
                        wx.navigateBack({
                          delta: 1
                        });
                      },1500)
                    }
                  })
                }
              },(err)=>{console.log(err)})
            }
          }
        }, (err)=>{console.log(err)})
      }
    })
  },
  terminate(){
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定中止该活动吗？',
      success: function(res) {
        var HeartActivityDetail = {
          statusflag: 3,
          endtime: formatTime(new Date()),
          activityid: that.data.activity.activityid
        }
        app.sendPostRequest("/heart/activity/finish", HeartActivityDetail, (res)=>{
          console.log("更新活动",res)
          if(res == 1){
            console.log(that.data.nowPartId)
            if(that.data.nowPartId!=null){
              let HeartActivityPart = {
                id: that.data.nowPartId,
                statusflag: 3,
                outtime:formatTime(new Date()),
                activityid: that.data.activity.activityid,
                participatorid: that.data.nowParterId
              }
            console.log("HeartActivityPart",HeartActivityPart)

              app.sendPostRequest("/heart/activity/updataNowPart", HeartActivityPart, (res)=>{
                console.log("更新参与者",res)
                if(res == 1){
                  wx.showToast({
                    title: '中止成功',
                    duration: 1500,
                    success:function () {
                      setTimeout(function(){
                        var pages = getCurrentPages(); //当前页面
                        var beforePage = pages[pages.length - 2]; //前一页
                        beforePage.onLoad(); // 执行前一个页面的onLoad方法
                        wx.navigateBack({
                          delta: 1
                        });
                      },1500)
                    }
                  })
                }
              },(err)=>{console.log(err)})
            }
          }
        }, (err)=>{console.log(err)})
      }
    })
  }, 
  jubao: function(e){
    this.setData({
      showP: true
    })
  },
  onClick(e){
    var target = e.target.dataset.name
    var v = 'accuseForm.type'
    this.setData({
      [v]:target
    })
    console.log(this.data.accuseForm)
  },
  //举报表单的文字输入绑定
  accuseFormInputChange(e){
    var v = 'accuseForm.memo'
    this.setData({
      [v]:e.detail
    })
    console.log(this.data.accuseForm)
  },
  submitAccuse(){
    var that = this
    if(this.data.accuseForm.type==null){
      wx.showToast({
        title: '举报类型为必选项',
        icon:'error'
      })
      return;
    }
    app.sendPostRequest('/system/report',this.data.accuseForm,(res)=>{
      if(res.code==300){
        wx.showToast({
          title: '您已举报过该帖子',
          icon:'error'
        })
      }else{
        wx.showToast({
          title: '举报成功',
          icon:'success'
        })
      }
      that.setData({
        showP:false
      })
    },(err)=>{
      console.log(err)
    })
  },
  dePart(){  //退出活动
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定退出该活动吗？',
      success: function(res){
        let HeartActivityPart = {
          id: that.data.nowPartId,
          statusflag: 1 ,
          outtime:formatTime(new Date()),
          activityid: that.data.activity.activityid,
        }
        app.sendPostRequest("/heart/activity/updataNowPart", HeartActivityPart, (res)=>{
          console.log("更新参与者",res)
          if(res == 1){
            var HeartActivityDetail = {
              statusflag: 1,
              endtime: formatTime(new Date()),
              activityid: that.data.activity.activityid
            }
            app.sendPostRequest("/heart/activity/finish", HeartActivityDetail, (res)=>{
              if(res == 1){
                wx.showToast({
                  title: '退出成功',
                })
              }
            })
          }
        },(err)=>{console.log(err)})
      }
    })
  },
  editActivity: function (e){
    wx.navigateTo({
      url: '../editActivity/editActivity?activityId=' + this.data.activity.activityId,
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let pages = getCurrentPages();
    let prePage = pages[pages.length-2];
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})